package com.zheng.study.web.controller.main.company;

import com.zheng.study.base.bean.Result;
import com.zheng.study.company.entity.Company;
import com.zheng.study.company.entity.User;
import com.zheng.study.company.service.CompanyService;
import com.zheng.study.web.controller.BaseController;
import com.zheng.study.web.shiro.bind.annotation.CurrentUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 企业信息
 * ============================================================================
 * author : dzw
 * createDate:  2018/11/22 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/main/company")
public class CompanyController extends BaseController {
	private static final long serialVersionUID = 6687051610562393240L;

	@Autowired
	private CompanyService companyService;

	/**
	 * 首页
	 */
	@RequestMapping(value = "view", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
	public String view(@CurrentUser User user, Model model){
		model.addAttribute("logoUrl", companyService.getCompanyLogoUrl(user.getCompany()));
		model.addAttribute("company", user.getCompany());
		return "/main/menu/company/company";
	}

	/**
	 * logoDialog
	 */
	@RequestMapping(value = "logoDialog", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
	public String logoDialog(@CurrentUser User user, Model model, String companyId){
		Company company = companyService.get(companyId);
		if(company==null){
			company = user.getCompany();
		}

		model.addAttribute("logoUrl", companyService.getCompanyLogoUrl(company));
		model.addAttribute("logoName", "logo_"+company.getName());
		return "/main/company/logo_tailor_dialog";
	}



	/**
	 *  保存
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(@CurrentUser User user, String keyId, String logoFileId, String name, String subName, String place, String address, String description){
		if(StringUtils.isBlank(keyId)){
			return ajaxJson(resultService.fail("未找到当前企业"));
		}

		Company company = companyService.get(keyId);
		if(company==null || !company.equals(user.getCompany())){
			return ajaxJson(resultService.fail("您不是当前企业的成员"));
		}

		company.setLogoFileId(logoFileId);
		company.setName(name);
		company.setSubName(subName);
		company.setAddress(address);
		company.setDescription(description);

		company.setProvince(null);
		company.setCity(null);
		company.setCounty(null);
		if(StringUtils.isNotBlank(place)){
			String[] placeArr = place.split("/");
			if(placeArr.length>0){
				company.setProvince(placeArr[0]);
			}
			if(placeArr.length>1){
				company.setCity(placeArr[1]);
			}
			if(placeArr.length>2){
				company.setCounty(placeArr[2]);
			}
		}
		companyService.update(company);

		return ajaxJson(resultService.success("保存成功"));
	}



}
